﻿/// <reference path="_namespace.js" />
/// <reference path="../../components/component.js" />
/// <reference path="../../events/EventRouter.js" />
/// <reference path="IGameComponent.js" />
/// <reference path="IUpdateable.js" />


/*****************************************************************************
Ajaxna library class definition. Inheriting from ajax.components.Component.
Author: krobinson 10/13/2008 4:33:50 PM.
Class:  ajax.xna.framework.GameComponent
*****************************************************************************/

$addNamespace("ajax.xna.framework");
//-----------------------------------------------------------------------------

ajax.xna.framework.GameComponent = function(game)
{///<summary>Base class for all XNA Framework game components.</summary>
 ///<param name="game" type="ajax.xna.framework.Game">Game that the game component should be attached to.</param>
    ajax.xna.framework.GameComponent.superConstructor.call(this, game.getDocument());
    this._game = game;
    this._enabled = true;
    this._updateOrder = 0;
    // Events
    this.getEvent(this.getWindow(), "onEnabledChanged");
    this.getEvent(this.getWindow(), "onUpdateOrderChanged");
    
}
$extend("ajax.xna.framework.GameComponent", "ajax.components.Component");
$supports("ajax.xna.framework.GameComponent", "ajax.xna.framework.IGameComponent");
$supports("ajax.xna.framework.GameComponent", "ajax.xna.framework.IUpdateable");

if ($getDesignMode())
    ajax.xna.framework.GameComponent.prototype = ajax.components.Component.prototype;
//-----------------------------------------------------------------------------

// Members
//-----------------------------------------------------------------------------
ajax.xna.framework.GameComponent.prototype._enabled = true;
ajax.xna.framework.GameComponent.prototype._updateOrder = 0;
ajax.xna.framework.GameComponent.prototype._game = null;


// Public Methods
//-----------------------------------------------------------------------------
ajax.xna.framework.GameComponent.prototype.get_game = function() 
{///<summary>Returns the instance of the ajax.xna.framework.Game object this component belongs to.</summary>
 ///<returns type="ajax.xna.framework.Game"/>
    return this._game; 
}
//-----------------------------------------------------------------------------
ajax.xna.framework.GameComponent.prototype.set_enabled = function(value)
{///<summary>
/// Indicates whether the game component's IUpdateable.update() method should be called in Game.update().    
///</summary>
///<param name="value" type="bool">True if update() should be called; false otherwise.</param>
    if (this._enabled != value)
    {
        this._enabled = value;
        this.onEnabledChanged.call(this);
    }
}
//-----------------------------------------------------------------------------
ajax.xna.framework.GameComponent.prototype.set_updateOrder = function(value)
{///<summary>Indicates when the game component should be updated relative to other game components. Lower values are updated first.</summary>
 ///<param name="value" type="int" integer="true">When the game component should be updated relative to other components.</param>  
    if (this._updateOrder != value)
    {
        this._updateOrder = value;
        this.onUpdateOrderChanged.call(this);  
    }
}


// Supports IGameComponent
//-----------------------------------------------------------------------------
ajax.xna.framework.GameComponent.prototype.initialize = function()
{///<summary>
    /// Called when the GameComponent needs to be initialized. Override this method
    /// to query for required services and load any non-graphics resources. No need to call base.
 ///</summary>
}

// Supports IUpdateable
//-----------------------------------------------------------------------------
ajax.xna.framework.GameComponent.prototype.get_enabled = function()
{///<summary>Indicates whether the game component's IUpdateable.update() method should be called in Game.update().</summary>
 ///<returns type="bool">True if IUpdateable.update() should be called; false otherwise.</returns> 
    return this._enabled;    
}
//-----------------------------------------------------------------------------
 ajax.xna.framework.GameComponent.prototype.add_enabledChanged = function(handler)
{///<summary>Adds handler for event raised when the IUpdateable.get_enabled() property changes.</summary>
    this.getEvent(this.getWindow(), "onEnabledChanged").addListener(handler);
}
//-----------------------------------------------------------------------------
ajax.xna.framework.GameComponent.prototype.remove_enabledChanged    = function(handler)
{///<summary>Removes handler for event raised when the IUpdateable.get_enabled() property changes.</summary>
    this.getEvent(this.getWindow(), "onEnabledChanged").removeListener(handler);
}
//-----------------------------------------------------------------------------
ajax.xna.framework.GameComponent.prototype.get_updateOrder = function()
{///<summary>Indicates when the game component should be updated relative to other game components. Lower values are updated first.</summary>
    ///<returns type="int" integer="true">When the game component should be updated.</returns>
    return this._updateOrder;
}
//-----------------------------------------------------------------------------
ajax.xna.framework.GameComponent.prototype.add_updateOrderChanged = function(handler)
{///<summary>Adds handler for event raised when the IUpdateable.get_updateOrder() property changes.</summary>
    this.getEvent(this.getWindow(), "onUpdateOrderChanged").addListener(handler);
}
//-----------------------------------------------------------------------------
ajax.xna.framework.GameComponent.prototype.remove_updateOrderChanged = function(handler)
{///<summary>Removes handler for event raised when the IUpdateable.get_updateOrder() property changes.</summary>
    this.getEvent(this.getWindow(), "onUpdateOrderChanged").addListener(handler);
}

//-----------------------------------------------------------------------------
ajax.xna.framework.GameComponent.prototype.update = function(gameTime)
{///<summary>Called when the GameComponent needs to be updated. Override this method with component-specific update code. No need to call base.</summary>
 ///<param name="gameTime" type="ajax.drawing.Timer"/>Time elapsed since the last call to update().</param>  
}

ajax.xna.framework.GameComponent.prototype.dispose = function()
{///<summary>Frees up memory of DOM objects created. Call base.</summary>
    ajax.xna.framework.GameComponent.superClass.dispose.call(this);
}


// Statics
//-----------------------------------------------------------------------------
ajax.xna.framework.GameComponent.cast = function(obj)
{///<returns type="ajax.xna.framework.GameComponent"/>
    return obj;
}